<?php
/*------------------------------------------------------------------------
 * WeixinController.class.php 
 *
 * 微信支付回调接口
 * 	
 * Created on 2016-07-18
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2015 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace Home\Controller;
class WeixinPayController extends CommonController{

	public function index(){}
	// 异步回调处理
    public function notify_url(){
    	//去掉TP的GET参数，否则可能影响计算验证结果
		unset($_GET['_URL_'], $_POST['_URL_'], $_REQUEST['_URL_']);
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');

        import("WeixinPay", 'Public/Class/Pay/WxPay/');
		$weixin_pay = new \WeixinPay();
		$weixin_pay->appid     = $pay_config['weixin_web_appid'];
		$weixin_pay->mchid     = $pay_config['weixin_web_mchid'];
		$weixin_pay->key       = $pay_config['weixin_web_key'];
		$weixin_pay->appsecret = $pay_config['weixin_web_appsecret'];
		$weixin_pay->cert_path = D('File')->get_file_info($pay_config['weixin_cert_file'], 'filepath');
		$weixin_pay->key_path  = D('File')->get_file_info($pay_config['weixin_key_file'], 'filepath');

		$this->log_path = $weixin_pay->logs_path . 'weixin_web_' . date('Y_m_d') . '.log';
		// 初始化配置
		$weixin_pay->init_config();

		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$xml = $xml ? $xml : file_get_contents("php://input");
		$this->logs('[异步]微信==>POST数据：' . print_r($_POST, true));
		$this->logs('[异步]微信==>GET数据：' . print_r($_GET, true));
		$this->logs('[异步]微信==>RAW_POST数据：' . $xml);

		//使用通用通知接口
		$notify = new \Notify_pub();
		$notify->saveData($xml);
		$post_data = $notify->data;
		$this->logs('[异步]微信==>RAW_POST数据：' . print_r($post_data, true));

		if($notify->checkSign() == TRUE){
			if ($post_data["return_code"] == "SUCCESS") {
				$this->logs('[异步]微信==>验证成功');
				$order_sn     = $post_data['out_trade_no'];//订单号
				$trade_no     = $post_data['transaction_id'];//微信支付订单号
				$total_fee    = $post_data['total_fee'] / 100;//支付金额
				$buyer_email  = $post_data['openid'];//买方支付账号
				$buyer_id     = $post_data['openid'];//买方ID
				$seller_email = $post_data['appid'];//卖方账号

				//提交订单支付
				$is_ok = D('Order')->order_pay(
					'weixin',
					$order_sn,
					$trade_no,
					$total_fee,
					$buyer_email,
					$buyer_id,
					$seller_email
				);
				if($is_ok){
					$this->logs('[异步]微信==>支付成功！');
					$this->return_xml('SUCCESS');
				}
				$this->logs('[异步]微信==>支付失败：' . D('Order')->tips_info);
				$this->return_xml('FAIL','支付失败');
			}else{
				$this->logs('[异步]微信==>验证失败');
				$this->return_xml('FAIL','验证失败');
			}
		}else{
		    //签名失败
		    $this->logs('[异步]签名失败');
		    $this->return_xml('FAIL','签名失败');
		}
    }
    // 异步回调处理
    public function recharge_notify_url(){
    	//去掉TP的GET参数，否则可能影响计算验证结果
		unset($_GET['_URL_'],$_POST['_URL_'],$_REQUEST['_URL_']);
		// 支付配置
		$pay_config = D('Config')->get('', 'pay');

        import("WeixinPay", 'Public/Class/Pay/WxPay/');
		$weixin_pay = new \WeixinPay();
		$weixin_pay->appid     = $pay_config['weixin_web_appid'];
		$weixin_pay->mchid     = $pay_config['weixin_web_mchid'];
		$weixin_pay->key       = $pay_config['weixin_web_key'];
		$weixin_pay->appsecret = $pay_config['weixin_web_appsecret'];
		$weixin_pay->cert_path = D('File')->get_file_info($pay_config['weixin_cert_file'], 'filepath');
		$weixin_pay->key_path  = D('File')->get_file_info($pay_config['weixin_key_file'], 'filepath');

		$this->log_path = $weixin_pay->logs_path . 'weixin_web_' . date('Y_m_d') . '.log';
		// 初始化配置
		$weixin_pay->init_config();

		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$xml = $xml ? $xml : file_get_contents("php://input");
		$this->logs('[异步]微信==>充值订单POST数据：' . print_r($_POST, true));
		$this->logs('[异步]微信==>充值订单GET数据：' . print_r($_GET, true));
		$this->logs('[异步]微信==>充值订单RAW_POST数据：' . $xml);
		//使用通用通知接口
		$notify = new \Notify_pub();
		$notify->saveData($xml);
		$post_data = $notify->data;
		$this->logs('[异步]微信==>充值订单RAW_POST数据：' . print_r($post_data, true));

		if($notify->checkSign() == TRUE){
			if ($post_data["return_code"] == "SUCCESS") {
				$this->logs('[异步]微信==>充值订单验证成功');
				$order_sn     = $post_data['out_trade_no'];//订单号
				$trade_no     = $post_data['transaction_id'];//微信支付订单号
				$total_fee    = $post_data['total_fee'] / 100;//支付金额
				$buyer_email  = $post_data['openid'];//买方支付账号
				$buyer_id     = $post_data['openid'];//买方ID
				$seller_email = $post_data['appid'];//卖方账号

				//提交订单支付
				$is_ok = D('Order')->recharge_order_pay(
					'weixin',
					$order_sn,
					$trade_no,
					$total_fee,
					$buyer_email,
					$buyer_id,
					$seller_email
				);
				if($is_ok){
					$this->logs('[异步]微信==>充值订单支付成功！');
					$this->return_xml('SUCCESS');
				}
				$this->logs('[异步]微信==>充值订单支付失败：' . D('Order')->tips_info);
				$this->return_xml('FAIL','充值订单支付失败');
			}else{
				$this->logs('[异步]微信==>充值订单验证失败');
				$this->return_xml('FAIL','充值订单验证失败');
			}
		}else{
		    //签名失败
		    $this->logs('[异步]充值订单签名失败');
		    $this->return_xml('FAIL','充值订单签名失败');
		}
    }
	//写入日志
	protected function logs($content){
		\Think\Log::write($content, \Think\Log::INFO, '', $this->log_path);
	}
	/**
	 * 返回给微信信息
	 * @param  string $status 处理状态：成功SUCCESS，失败FAIL
	 * @param  string $msg    处理结果：如OK
	 */
	public function return_xml($status = 'SUCCESS', $msg = 'OK'){
		$xml = '<xml><return_code><![CDATA[' . $status . ']]></return_code><return_msg><![CDATA[' . $msg . ']]></return_msg></xml>';
		exit($xml);
	}
}